﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace SinacorWebTools.ResourceAccess
{
    public class Modulo : DataAccess
    {
        public string SelecionaSiglaModulosPorDireito(int codigoDireito)
        {
            List<Entity.Modulo> modulos = new ResourceAccess.Modulo().ListaModulos(codigoDireito);

            string siglaModulos = string.Empty;

            if (modulos.Count > 0)
                siglaModulos = (from m in modulos select m.Sigla).ToList().Aggregate((first, second) => first + "," + second);

            return siglaModulos;
        }
        
        public List<Entity.Modulo> ListaModulos()
        {
            List<Entity.Modulo> modulos = new List<Entity.Modulo>();
            conn = CreateConnection();

            try
            {
                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT CD_SISTEMA FROM TFWMODULOSINST";
                conn.Open();

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                        modulos.Add(new Entity.Modulo(dr["CD_SISTEMA"].ToString()));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }

            return modulos;
        }

        public List<Entity.Modulo> ListaModulos(int cod_direitoAcesso)
        {
            List<Entity.Modulo> modulos = new List<Entity.Modulo>();
            DbConnection conn = CreateConnection();

            try
            {
                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT COD_SIST FROM TFWDIR_MOD_SIST WHERE COD_DIR = " + cod_direitoAcesso;
                conn.Open();

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                        modulos.Add(new Entity.Modulo(dr["COD_SIST"].ToString()));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }

            return modulos;
        }

        public int IncluiModulos(List<Entity.Modulo> modules)
        {
            int linhasAfetadas = 0;
            DbConnection conn = CreateConnection();

            try
            {
                DbCommand cmd = conn.CreateCommand();
                conn.Open();

                foreach (Entity.Modulo module in modules)
                {
                    cmd.CommandText = @"
								INSERT INTO 
									TFWDIR_MOD_SIST (COD_DIR, COD_TIPO_DIR, COD_SIST, TXT_HASH)
								VALUES 
									(" + module.CodigoDireito + ", " + module.CodigoTipoDireito + ", '" + module.Sigla + "', '" + module.TextoHash + @"')";

                    linhasAfetadas += cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }

            return linhasAfetadas;
        }

        public int ExcluiModulo(Entity.DireitoAcesso direito)
        {
            int linhasAfetadas = 0;
            DbConnection conn = CreateConnection();

            try
            {
                DbCommand cmd = conn.CreateCommand();
                conn.Open();

                cmd.CommandText = @"DELETE FROM TFWDIR_MOD_SIST WHERE COD_DIR = " + direito.CodigoDireito.ToString();

                linhasAfetadas = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }

            return linhasAfetadas;
        }
    }
}
